package com.sunwayhorizo.health.platform.security.dao.impl;

import java.util.List;

import javax.ejb.Stateless;

import org.jboss.seam.annotations.Name;

import com.sunwayhorizo.health.platform.base.BaseManager;
import com.sunwayhorizo.health.platform.common.PlatformConstant;
import com.sunwayhorizo.health.platform.security.dao.SecurityRoleDao;
import com.sunwayhorizo.health.platform.security.persistent.SecurityRole;

@Stateless
@Name("SecurityRoleDao")
public class SecurityRoleImpl extends BaseManager implements SecurityRoleDao{
	
	@Override
	public SecurityRole newSecurityRole(SecurityRole role) {
	    role = super.entityManager2.merge(role);
		return role;
	}
	
	@Override
	public void deleteSecurityRole(List<String> list) {
		super.delete(SecurityRole.class,list);
	}
	
	@Override
	public SecurityRole updateSecurityRole(SecurityRole role) {
		super.update(role);
		return role;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SecurityRole> findSecurityRoleList() {
		return super.entityManager2.createNamedQuery("SecurityRole.findAll").getResultList();
	}
	
	@SuppressWarnings("unchecked")
    @Override
    public List<SecurityRole> findSecurityRoleListWithOutRootNode() {
        return super.entityManager2.createQuery("select r from SecurityRole r where r.roleId != :rootId").setParameter("rootId", PlatformConstant.ROOT_ROLE).getResultList();
    }

	@SuppressWarnings("unchecked")
	@Override
	public List<SecurityRole> findSecurityRoleListWithFatherRole() {
		return super.entityManager2.createQuery("select distinct sr from SecurityRole sr left join fetch sr.fatherRole").getResultList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SecurityRole> findSecurityRoleListByAccountIdWithScreen(String accountId) {
		String jpql = "select distinct ar.role from SecurityAccountRole ar " +
					  "where ar.account.accountId = :accountId " +
					  "and not exists( " +
					  "		select sarp.account.accountId from SecurityAccountRolePermission sarp " +
					  " 	where sarp.account.accountId = :accountId" +
					  "		and ar.role = sarp.role " +
					  ") ";
		return super.entityManager2.createQuery(jpql).setParameter("accountId", accountId).getResultList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SecurityRole> findSecurityRoleListByAccountIdWithOutScreen(String accountId) {
		String jpql = "select distinct ar.role from SecurityAccountRole ar " +
					  "where ar.account.accountId = :accountId ";
		return super.entityManager2.createQuery(jpql).setParameter("accountId", accountId).getResultList();
	}
	
	@Override
	public SecurityRole findSecurityRoleDetailById(String roleId) {
		return super.find(SecurityRole.class, roleId);
	}
	
	@Override
	public SecurityRole findSecurityRoleDetailByName(String roleName) {
		return (SecurityRole)super.entityManager2.createQuery("select sr from SecurityRole sr where sr.roleName = :roleName").setParameter("roleName", roleName).getSingleResult();
	}
}
